package com.tsingteng.cosfun.utils;

import android.app.Activity;
import android.database.Cursor;
import android.provider.MediaStore;

import com.tsingteng.cosfun.bean.VideoLocalBean;
import com.tsingteng.cosfun.helper.VideoHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/10/19
 * qq(872179966)
 */

public class VideoLocalUtils {

    private static VideoLocalUtils INSTANCE = null;
    private List<VideoLocalBean> videoLocalBeanList = new ArrayList<>();

    private VideoLocalUtils() {
    }


    public static VideoLocalUtils getInstance() {
        if (INSTANCE == null) {
            synchronized (VideoLocalUtils.class) {
                if (INSTANCE == null) {
                    INSTANCE = new VideoLocalUtils();
                }
            }
        }
        return INSTANCE;
    }


    /**
     * 得到本地的视屏
     *
     * @param activity
     */
    public void getLocalVideoList(Activity activity) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (this != null) {
                    List<VideoLocalBean> list = null;
                    Cursor cursor = activity.getContentResolver().query(
                            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                            null, null);
                    if (cursor != null) {
                        list = new ArrayList<VideoLocalBean>();
                        while (cursor.moveToNext()) {
                            String title = cursor
                                    .getString(cursor
                                            .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                            String path = cursor
                                    .getString(cursor
                                            .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                            long duration = cursor
                                    .getInt(cursor
                                            .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                            long size = cursor
                                    .getLong(cursor
                                            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                            VideoLocalBean video = new VideoLocalBean();
                            video.setVideoCover(BitmapUtils.saveBitmap(VideoHelper.getIntence().getVideoThumbnail(path)));
                            video.setName(title);
                            video.setSize(size);
                            video.setUrl(path);
                            video.setDuration(duration);
                            list.add(video);
                        }
                        cursor.close();
                    }
                    setVideoLocalBeanList(list);
                }
            }
        }).start();
    }


    public void setVideoLocalBeanList(List<VideoLocalBean> list) {
        this.videoLocalBeanList = list;
    }


    public List<VideoLocalBean> getVideoLocalBeanList() {
        return videoLocalBeanList;
    }

}
